package com.devp.interceptor;

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @program: sany-heavy-energy
 * @description: 拦截参数
 * @author: yong.liu
 * @create: 2020-06-03
 **/
@Component
public class ParamInterceptor extends AbstractPhaseInterceptor<Message> {

    private final Logger logger = LoggerFactory.getLogger(ParamInterceptor.class);
    @Value("${wsdl.url}")
    private String url;

    public ParamInterceptor() {
        super(Phase.RECEIVE);
    }

    @Override
    public void handleMessage(Message message) {
        logger.info("********************* webservice in interceptor start  ************************");
        message.put("org.apache.cxf.request.url", url);

        InputStream is = message.getContent(InputStream.class);
        if (is != null) {
            try {
                String str = IOUtils.toString(is);

                logger.info(" webservice 入参报文：\r\n" + str);

                InputStream ism = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
                message.setContent(InputStream.class, ism);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        logger.info("********************* webservice in interceptor end  ************************");
    }
}
